﻿using System;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using MayhemCore;
using Microsoft.Phone.Controls;
using System.Reflection;
using MayhemCore.ModuleTypes;

namespace MayhemWP7
{
    public partial class MainPage : PhoneApplicationPage
    {
        public ObservableCollection<Error> Errors
        {
            get { return (ObservableCollection<Error>)GetValue(ErrorsProperty); }
            set { SetValue(ErrorsProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Errors.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ErrorsProperty =
            DependencyProperty.Register("Errors", typeof(ObservableCollection<Error>), typeof(MainPage), new PropertyMetadata(new ObservableCollection<Error>()));

        private Mayhem mayhem;

        // Constructor
        public MainPage()
        {
            InitializeComponent();
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            mayhem = Mayhem.Instance;
            RunList.ItemsSource = mayhem.ConnectionList;
        }

        /// <summary>
        /// User clicked on the New Connection button in the application bar
        /// </summary>
        private void NewConnectionButton_Click(object sender, EventArgs e)
        {
            // Go to the page for creating a new connection
            NavigationService.Navigate(new Uri("/NewConnection.xaml", UriKind.RelativeOrAbsolute));
        }

        private void RunList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ListBox l = sender as ListBox;

            
            //Connection c = (Connection)l.SelectedItem;
            NavigationService.Navigate(new Uri("/Configure.xaml?id="+l.SelectedIndex, UriKind.RelativeOrAbsolute));
            l.SelectedIndex = -1;
        }
    }
}